﻿package kliment.geom.color {
	import kliment.math.Calc;
	
	/**
	* ...
	* @author Kliment
	*/
	public class CMYKColor extends ColorAbstract {
		private const WHITE:int = 100;
		
		public var rawM:Number;
		public var rawK:Number;
		public var rawY:Number;
		public var rawC:Number;
		
		public function CMYKColor(host:Color = null) {
			super(host);
		}
		
		public function get c():Number { return rawC * WHITE }
		public function get m():Number { return rawM * WHITE }
		public function get y():Number { return rawY * WHITE }
		public function get k():Number { return rawK * WHITE }
		
		public function setValue(c:Number, m:Number, y:Number, k:Number, update:Boolean = true):void {
			setRawValue(c / WHITE, m / WHITE, y / WHITE, k / WHITE, update);
		}
		
		public function setRawValue(c:Number, m:Number, y:Number, k:Number, update:Boolean = true):void {
			if (rawC == c && rawM == m && rawY == y && rawK == k)
				return;
			
			rawC = Calc.inInterval(MIN_RAW, MAX_RAW, c);
			rawM = Calc.inInterval(MIN_RAW, MAX_RAW, m);
			rawY = Calc.inInterval(MIN_RAW, MAX_RAW, y);
			rawK = Calc.inInterval(MIN_RAW, MAX_RAW, k);
			
			if (color && update)
				color.valueChanged(Color.CMYK);
		}
		
		public function toString():String {
			return "[CMYKColor c=" + c + " m=" + m + " y=" + y + " k=" + k +"]";
		}
		
		public function clone():CMYKColor {
			var result:CMYKColor = new CMYKColor();
			result.setRawValue(rawC, rawM, rawY, rawK, false);
			return result;
		}
		
		public function toRGB(rgb:RGBColor = null):RGBColor { return converToRGB(this, rgb) }
		static public function converToRGB(cmyk:CMYKColor, rgb:RGBColor = null):RGBColor {
			if (!rgb) rgb = new RGBColor();
			var rawK:Number = cmyk.rawK;
			var _rawK:Number = 1 - rawK;
			rgb.setRawValue(1 - (cmyk.rawC * _rawK + rawK), 1 - (cmyk.rawM * _rawK + rawK), 1 - (cmyk.rawY * _rawK + rawK), false);
			return rgb;
		}
	}
}